<?php

/**
 * sfProducts actions.
 *
 * @package    blog
 * @subpackage sfProducts
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sfProductsActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->shop_products = Doctrine_Core::getTable('ShopProduct')->getActiveProducts();
      
      
      $this->shop_categs = Doctrine_Core::getTable('ShopCategory')->getWithProducts();
      
      
   $this->pager = new sfDoctrinePager('ShopProduct', sfConfig::get('app_max_articles_by_category_page'));
  $this->pager->setQuery(Doctrine::getTable('ShopProduct')->getActiveProducts2());
  $this->pager->setPage($request->getParameter('page', 1));
  $this->pager->init(); 
      
      
      
      
      
      
      $this->chemin = sfContext::getInstance()->getRequest()->getUri();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->shop_product = Doctrine_Core::getTable('ShopProduct')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->shop_product);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ShopProductForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ShopProductForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($shop_product = Doctrine_Core::getTable('ShopProduct')->find(array($request->getParameter('id'))), sprintf('Object shop_product does not exist (%s).', $request->getParameter('id')));
    $this->form = new ShopProductForm($shop_product);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($shop_product = Doctrine_Core::getTable('ShopProduct')->find(array($request->getParameter('id'))), sprintf('Object shop_product does not exist (%s).', $request->getParameter('id')));
    $this->form = new ShopProductForm($shop_product);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($shop_product = Doctrine_Core::getTable('ShopProduct')->find(array($request->getParameter('id'))), sprintf('Object shop_product does not exist (%s).', $request->getParameter('id')));
    $shop_product->delete();

    $this->redirect('sfProducts/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $shop_product = $form->save();

      $this->redirect('sfProducts/edit?id='.$shop_product->getId());
    }
  }
}
